import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.Color;


public class ChildOf {

	public JFrame frmChoice;
	private DefaultListModel lm;
	private JList peopleList;
	private JLabel lblError;
	private JTextField searchField;
	private JButton btnSearch;
	
	int button;
	int buttonToWatch;
	ArrayList<Person> thePeople;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ChildOf window = new ChildOf();
					window.frmChoice.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ChildOf() {
		initialize();
		button = Relations.button;
		buttonToWatch = Relations.buttonToWatch;
		Database db = new Database();
		ArrayList<Person> p = db.getAllPersons();
		populate(p);
	}
	
	public void populate(ArrayList<Person> p){
		lm.clear();
		thePeople = new ArrayList<Person>();
		if(button != 6){
			for(int i = 0; i < p.size(); i++){
				if(!p.get(i).id.equals(MainScreen.personId)){
					if(Relations.people.containsKey(buttonToWatch)){
						if(!Relations.people.get(buttonToWatch).id.equals(p.get(i).id)){
							String name = p.get(i).fname + " " + p.get(i).lname;
							thePeople.add(p.get(i));
							lm.addElement(name);
						}
					}else{
						String name = p.get(i).fname + " " + p.get(i).lname;
						thePeople.add(p.get(i));
						lm.addElement(name);
					}
				}
			}
			if(button == 3){
				lm.addElement("None");
			}
		}else{
			for(int i = 0; i < p.size(); i++){
				if(!p.get(i).id.equals(MainScreen.personId)){
					String name = p.get(i).fname + " " + p.get(i).lname;
					thePeople.add(p.get(i));
					lm.addElement(name);
				}
			}
		}
	}
	
	public void searching(){
		ArrayList<Person> list;
		String str = searchField.getText();
		Database db = new Database();
		if(str.contains(" ")){
			String delims = "[ ]+";
			String[] names = str.split(delims);
			String first = "";
			String last = names[names.length-1];
			if(names.length > 2){
				for(int i = 0; i < names.length; i++){
					if(i != names.length - 1){
						if(i != names.length - 2){
							first = first + names[i] + " ";
						}else{
							first = first + names[i];
						}
					}
				}
			}else{
				first = names[0];
			}
			list = db.lookupName(first,last);
		}else{
			list = db.lookupName(str);
		}
		populate(list);
	}
	
	public void sendBack(){
		if(!peopleList.getSelectedValue().toString().equals("None")){
			Person p = thePeople.get(peopleList.getSelectedIndex());
			Relations.people.put(button, p);
			if(button == 1){
				Relations.par1.setText(p.fname + " " + p.lname);
			}else if(button == 2){
				Relations.par2.setText(p.fname + " " + p.lname);
			}else if(button == 3){
				Relations.spouse.setText(p.fname + " " + p.lname);
			}else if(button == 4){
				Relations.aPar1.setText(p.fname + " " + p.lname);
			}else if(button == 5){
				Relations.aPar2.setText(p.fname + " " + p.lname);
			}
		}else{
			Relations.people.remove(3);
		}
		frmChoice.setVisible(false);
	}
	
	public void divorce(){
		Database db = new Database();
		String desc = "Changed Divorce";
		boolean result = db.updateDivorced(MainScreen.personId,thePeople.get(peopleList.getSelectedIndex()).id);
		result = db.updateEdits(MainScreen.theUser, desc, MainScreen.personId);
		frmChoice.setVisible(false);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmChoice = new JFrame();
		frmChoice.setTitle("Choice");
		frmChoice.setBounds(100, 100, 244, 463);
		frmChoice.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmChoice.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frmChoice.getContentPane().setLayout(null);
		
		JScrollPane scrollFather = new JScrollPane();
		scrollFather.setBounds(22, 11, 189, 292);
		frmChoice.getContentPane().add(scrollFather);
		
		lm = new DefaultListModel();
		
		// male list
		peopleList = new JList(lm);
		scrollFather.setViewportView(peopleList);
		
		JButton btnNewButton = new JButton("Select");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  if(peopleList.getSelectedValue() != null){
					  if(button != 6){
						  sendBack();
					  }else{
						  divorce();
					  }
				  }
			}
		});
		btnNewButton.setBounds(132, 390, 79, 23);
		frmChoice.getContentPane().add(btnNewButton);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//cancel here!
			}
		});
		btnCancel.setBounds(22, 390, 89, 23);
		frmChoice.getContentPane().add(btnCancel);
		
		lblError = new JLabel("");
		lblError.setForeground(Color.RED);
		lblError.setBounds(359, 11, 65, 14);
		frmChoice.getContentPane().add(lblError);
		
		searchField = new JTextField();
		searchField.setBounds(22, 314, 189, 20);
		frmChoice.getContentPane().add(searchField);
		searchField.setColumns(10);
		
		btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searching();
			}
		});
		btnSearch.setBounds(69, 345, 89, 23);
		frmChoice.getContentPane().add(btnSearch);
		
		//motherList.setSelectedIndex(0);
		//maleList.setSelectedIndex(0);
		//childList.setSelectedIndex(0);
	}

	
	public void searchGender(String name, String gender){
		ArrayList<Person> list = new ArrayList<Person>();
		Database db = new Database();
		
	}
	
	public void search(String name){
		
	}
}
